package com.smedi.ismedi.inmail.core.port.adapter.service.impl;

import com.smedi.ismedi.file.dto.response.FileInfoResponse;
import com.smedi.ismedi.file.feign.RemoteFileFeignApi;
import com.smedi.ismedi.inmail.core.infrastructure.constant.AnnouncementError;
import com.smedi.ismedi.inmail.core.port.adapter.service.FileRemoteApiService;
import com.smedi.ismedi.web.starter.common.ResponseData;
import com.smedi.ismedi.web.starter.exception.BasicError;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * 远程文件接口实现类
 *
 * @author jiangqinxuan
 * @date 2021/07/27
 */
@Service
@AllArgsConstructor
public class FileRemoteApiServiceImpl implements FileRemoteApiService {
    private final RemoteFileFeignApi remoteFileFeignApi;

    @Override
    public List<FileInfoResponse> getFileInfoListByKeyList(List<String> KeyList) {
        List<FileInfoResponse> fileInfoResponseList = new ArrayList<>();

        for (String keyTemp : KeyList) {
            FileInfoResponse fileInfoResponse = remoteFileFeignApi.getFileInfo(keyTemp).getData();
            if (fileInfoResponse == null) {
                throw LogicException.raise(AnnouncementError.ATTACHMENT_INFO_MISS);
            }
            fileInfoResponseList.add(fileInfoResponse);
        }

        return fileInfoResponseList;
    }

    @Override
    public String uploadFile(File inputFile) throws IOException {
        FileItem fileItem = new DiskFileItem("file", Files.probeContentType(inputFile.toPath()), false, inputFile.getName(), (int) inputFile.length(), inputFile.getParentFile());
        try (OutputStream outputStream = fileItem.getOutputStream()) {
            outputStream.write(Files.readAllBytes(Paths.get(inputFile.getAbsolutePath())));
        }
        MultipartFile multipartFile = new CommonsMultipartFile(fileItem);

        // 上传文件
        ResponseData<FileInfoResponse> fileUpLoadResponseData = remoteFileFeignApi.fileUpLoad(multipartFile);
        if (!fileUpLoadResponseData.isSuccess()) {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, fileUpLoadResponseData.getApplication(), String.valueOf(fileUpLoadResponseData.getErrorMsg()));
        }

        return fileUpLoadResponseData.getData().getSignKey();
    }

    @Override
    public FileInfoResponse uploadFileGetResponse(File inputFile) throws IOException {
        FileItem fileItem = new DiskFileItem("file", Files.probeContentType(inputFile.toPath()), false, inputFile.getName(), (int) inputFile.length(), inputFile.getParentFile());
        try (OutputStream outputStream = fileItem.getOutputStream()) {
            outputStream.write(Files.readAllBytes(Paths.get(inputFile.getAbsolutePath())));
        }
        MultipartFile multipartFile = new CommonsMultipartFile(fileItem);

        // 上传文件
        ResponseData<FileInfoResponse> fileUpLoadResponseData = remoteFileFeignApi.fileUpLoad(multipartFile);
        if (!fileUpLoadResponseData.isSuccess()) {
            throw LogicException.raise(BasicError.CALL_REMOTE_API_ERROR, fileUpLoadResponseData.getApplication(), String.valueOf(fileUpLoadResponseData.getErrorMsg()));
        }

        return fileUpLoadResponseData.getData();
    }
}
